/**
 * Calculate the data that's needed to draw the image in the image editor on the
 * resulting image. E.g. remove left part of the image, resize the image, move the image, ...
 */
function calculateDrawData(args) {
	//left offset of the image element
	var imageLeft = args[0];
	//width of the image element
	var imageWidth = args[1];
	//top offset of the image element
	var imageTop = args[2];
	//height of the image element
	var imageHeight = args[3];
	var imageRight = imageLeft + imageWidth;
	var imageBottom = imageTop + imageHeight;

	//left offset of the container element
	var containerLeft = args[4];
	//width of the container element
	var containerWidth = args[5];
	//top offset of the container element
	var containerTop = args[6];
	//height of the container element
	var containerHeight = args[7];
	var containerRight = containerLeft + containerWidth;
	var containerBottom = containerTop + containerHeight;

	//the effective width of the given image
	var realImageWidth = args[8];
	//the effective height of the given image
	var realImageHeight = args[9];

	sourceX = -1;
	sourceY = -1;
	sourceWidth = -1;
	sourceHeight = -1;
	destX = -1;
	destY = -1;
	destWidth = -1;
	destHeight = -1;

	if (overlaps(imageWidth, containerWidth, imageHeight, containerHeight, imageLeft, containerLeft, imageTop, containerTop)) {
		//image boundaries are the same as the ones from the canvas
		sourceX = imageLeft;
		sourceY = imageTop;
		sourceWidth = imageWidth;
		sourceHeight = imageHeight;
		destX = 0;
		destY = 0;
		destWidth = imageWidth;
		destHeight = imageHeight;

	} else {
		if (objectResized(realImageWidth, realImageHeight, imageWidth, imageHeight)) {

		} else {
			//object is not resized
			if (getMaximum(imageLeft, containerRight) == imageLeft) {
				sourceX = 0;
				sourceY = 0;
				sourceWidth = 0;
				sourceHeight = 0;
				destX = 0;
				destY = 0;
				destWidth = 0;
				destHeight = 0;
			} else {
				sourceX = ((imageLeft < containerLeft) ? containerLeft - imageLeft : 0);
				sourceY = getMaximum(imageTop, containerTop);
				sourceWidth = getMinimum(imageRight, containerRight) - getMaximum(imageLeft, containerLeft);
				sourceHeight = getMinimum(imageBottom, containerBottom) - getMaximum(imageTop, containerTop);
				destX = getMaximum(imageLeft, containerLeft) - containerLeft;
				destY = getMaximum(imageTop, containerTop) - containerTop;
				destWidth = containerRight - getMaximum(imageLeft, containerLeft);
				destHeight = getMinimum(imageBottom, containerBottom) - containerTop;
			}
		}
	}

	return [ sourceX, sourceY, sourceWidth, sourceHeight, destX, destY, destWidth, destHeight ];
}

function hasSameWidth(object1Width, object2Width) {
	return object1Width == object2Width;
}

function hasSameHeight(object1Height, object2Height) {
	return object1Height == object2Height;
}

function objectResized(oldWidth, oldHeight, newWidth, newHeight) {
	return !(hasSameWidth(oldWidth, newWidth) && hasSameHeight(oldHeight, newHeight));
}

function overlaps(object1Width, object2Width, object1Height, object2Height, object1Left, object2Left, object1Top, object2Top) {
	var notResized = !objectResized(object1Width, object2Height, object2Width, object2Height);
	return notResized && (object1Left == object2Left) && (object1Top == object2Top);
}

function getMinimum(value1, value2) {
	if (value1 < value2) {
		return value1;
	} else {
		return value2;
	}
}

function getMaximum(value1, value2) {
	if (value1 < value2) {
		return value2;
	} else {
		return value1;
	}
}